package register

import (
	"fmt"
	"github.com/hashicorp/consul/api"
)

type Registry struct {
	Host string
	Port int
}

type RegistryClient interface {
	Register(address string, port int, name string, tags []string, id string) error
	DeRegister(serverId string) error
}

//func (r *Registry) Register(address string, port int, name string, tags []string, id string) error {
//
//	config := api.DefaultConfig()
//	config.Address = fmt.Sprintf("%s:%d", r.Host, r.Port)
//	client, err := api.NewClient(config)
//	if err != nil {
//		return err
//	}
//	check := api.AgentServiceCheck{
//		HTTP:                           fmt.Sprintf("http://%s:%d/health", address, port),
//		Timeout:                        "5s",
//		Interval:                       "5s",
//		DeregisterCriticalServiceAfter: "15s",
//	}
//	registration := new(api.AgentServiceRegistration)
//	registration.Address = address
//	registration.Name = name
//	registration.Port = port
//	registration.Tags = tags
//	registration.ID = id
//	registration.Check = &check
//
//	err = client.Agent().ServiceRegister(registration)
//	if err != nil {
//		return err
//	}
//	return nil
//}

func (r *Registry) Register(address string, port int, name string, tags []string, id string) error {
	cfg := api.DefaultConfig()
	cfg.Address = fmt.Sprintf("%s:%d", r.Host, r.Port)

	client, err := api.NewClient(cfg)
	if err != nil {
		panic(err)
	}
	//生成对应的检查对象
	check := &api.AgentServiceCheck{
		HTTP:                           fmt.Sprintf("http://%s:%d/health", address, port),
		Timeout:                        "5s",
		Interval:                       "5s",
		DeregisterCriticalServiceAfter: "10s",
	}

	//生成注册对象
	registration := new(api.AgentServiceRegistration)
	registration.Name = name
	registration.ID = id
	registration.Port = port
	registration.Tags = tags
	registration.Address = address
	registration.Check = check

	err = client.Agent().ServiceRegister(registration)
	if err != nil {
		panic(err)
	}
	return nil
}
func (r Registry) DeRegister(serverId string) error {

	config := api.DefaultConfig()
	config.Address = fmt.Sprintf("%s:%d", r.Host, r.Port)
	client, err := api.NewClient(config)
	if err != nil {
		return err
	}
	err = client.Agent().ServiceDeregister(serverId)
	if err != nil {
		return err
	}
	return nil
}
